var pGrid, pGridData, opt, userid, udata,picking_data;
$(function() {
    //ldz 设置表单高度
    $("#form_core").height($(".form_tab").height()-$("#form_option").outerHeight());
    //ldz tab 从表切换事件
    $('#myTabs a').click(function (e) {
	  e.preventDefault()
	  $(this).tab('show')
	});
		
	// 明细列表设置
   	pGrid = $("#p-div").ligerGrid({
        columns:[
            {display:'授权类别',name:'licensetype',width:'10%'
            },
            {display:'授权名称',name:'licensename',width:'10%'
            }
        ], 
        pageSize:20, 
        width:'98%',//ldz
        height:'99%', 
        rownumbers:true,
        fixedCellHeight:false, 
        enabledEdit:true, 
        onSelectRow:function(rowdata, rowindex){$("#txtrowindex").val(rowindex);},
        param_partner:'partner_list',
        alternatingRow: true,
        clickToEdit: true,
        isScroll: false,
        usePager:false,
        editorTopDiff:4,
        editorLeftDiff:2
	});
   	
	var query = getQueryString(window.location.toString());
	opt = query.o;
	if (opt) {
		userid = query.userid;
		if (opt === 'm') {
			ajaxSubmitGet("/employeeCard/searchEmployeeCard/" + userid, function(data) {
				//绑定数据
				//编辑时值回显
				bindVal(data);
			});
		}
	} else {
		$("#createDateS").val(new Date().format("yyyy-MM-dd hh:mm:ss"));
	}

    $('[data-toggle="tooltip"]').tooltip();
    
	$("#pageloading").hide();
	
	autoGetCardIC();
});
function bindVal(data) {
	picking_data = data['data']; 
	//绑定主表数据 
	$('#userid').val(picking_data.userid);
	$('#cardType').val(picking_data.cardType);
	$('#create_address').val(picking_data.create_address);
	$('#create_date').val(picking_data.create_date);
	$('#ssn').val(picking_data.ssn);
	$('#name').val(picking_data.name);
	$('#sex').val(picking_data.sex);
	$('#address').val(picking_data.address);
	$('#mobile_phone').val(picking_data.mobile_phone);
	$('#fix_phone').val(picking_data.fix_phone);
	$('#company_id').val(picking_data.company_id);
	$('#dept_id').val(picking_data.dept_id);
	$('#cardid').val(picking_data.cardid);
	$('#create_uid').val(picking_data.create_uid);
	$('#card_valid').val(picking_data.card_valid);
	$('#cardDept').val(picking_data.carddept);

	//明细列表数据加载
	pGrid.setOptions({
		data : {
			Rows : picking_data['items']
		}
	}); //设置数据参数
	pGrid.loadData();//加载数据 
}
var tom1;
function autoGetCardIC(){
	clearTimeout(tom1);
	var yktkh = "";
	if(OpenDevice()){
		yktkh=RfRead();
		yktkh=yktkh.substr(yktkh.length-6);
	}
	if(yktkh != "undefined" && yktkh != undefined && yktkh != ""){
		window.location = './employeeCardDetail.html?o=m&userid='.concat(yktkh);
	}
	
	tom1=setTimeout("autoGetCardIC()",2000);
}


//--------------------------员工卡ocx方法开始-----------------------------
function OpenDevice() //打开设备
{
	var st = document.getElementById("CActiveXCtrl").OpenDevice();
	if ((st == 0 || st < 0) && document.getElementById("CActiveXCtrl").lErrorCode != 0) {
		document.all("DeviceHandle").value = "-1";
		$("#info").html("打开设备失败");
		return false;
	} else {
		document.all("DeviceHandle").value = st;
		return RfReset();
	}
}
function RfReset() {
	var resettime="10";
	var ret = document.getElementById("CActiveXCtrl").RfReset(document.all("DeviceHandle").value, resettime);
	if (document.getElementById("CActiveXCtrl").lErrorCode == 0) {
		return RfCard();
	} else {
		$("#info").html("请连接读卡器" );
		return false;
	}
}
function RfCard() {
	var ret = document.getElementById("CActiveXCtrl").RfCard(document.all("DeviceHandle").value, 0);
	if (document.getElementById("CActiveXCtrl").lErrorCode == 0) {
		return RfAuthenticationKey();
	} else {
		$("#info").html("寻卡中..." );
		return false;
	}
}
var defpsw="ffffffffffff";
function RfAuthenticationKey() {
	var keytype ="0"; //获取下拉框当前选值
	var ret = document.getElementById("CActiveXCtrl").RfAuthenticationKey(document.all("DeviceHandle").value, keytype, document.all("BlockM1").value, defpsw);
	if (document.getElementById("CActiveXCtrl").lErrorCode == 0) {
		//alert("校验密码成功");
		return true;
	} else {
		$("#info").html("校验密码失败" );
		return false;
	}
}



function RfBeep() {
	var ret = document.getElementById("CActiveXCtrl").RfBeep(document.all("DeviceHandle").value, 20);
}
function RfRead() {
	var ret = document.getElementById("CActiveXCtrl").RfRead(document.all("DeviceHandle").value, document.all("BlockM1").value);
	if (document.getElementById("CActiveXCtrl").lErrorCode == 0) {
		RfBeep();
		CloseDevice();
		return  ret; 
	} else {
		alert("读数据失败，错误码为：" + document.getElementById("CActiveXCtrl").lErrorCode);
		return "";
	}
}
function CloseDevice() //关闭设备
{
	var ret = document.getElementById("CActiveXCtrl").CloseDevice(document.all("DeviceHandle").value);
	document.all("DeviceHandle").value = "-1";
	if (ret == 0){
		//alert("关闭设备成功");
		return true;
	}	else{
		alert("关闭设备失败，错误码为：" + ret);
		return false;
	}
}
